package cn.nawang.ebeim.ui.controller;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;

import cn.nawang.ebeim.client.bean.ClientConfiguration;
import cn.nawang.ebeim.client.bean.StorageServer;
import cn.nawang.ebeim.client.util.CommonMethodsUtils;
import cn.nawang.ebeim.client.util.FileUtil;
import cn.nawang.ebeim.client.util.ZookeeperUtil;
import cn.nawang.ebeim.client.version.FileNode;
import cn.nawang.ebeim.client.version.Node;
import cn.nawang.ebeim.client.version.VersionTree;
import cn.nawang.ebeim.client.version.VersionTreeUtil;
import cn.nawang.ebeim.ui.bean.User;
import cn.nawang.ebeim.ui.util.ApiUtil;
import cn.nawang.ebeim.ui.util.UIversionTreeUtil;

/**
 * @ClassName: FileChangeLogController
 * @Description: TODO
 * @author fanyb
 * @date Nov 12, 2015 5:41:42 PM
 * @version v1.0
 */
@RestController
public class FileChangeLogController {
  private static final Logger LOG = LoggerFactory.getLogger(FileChangeLogController.class);
  
  @Autowired
  private HttpSession session;
  
  /**
   * @author zhangjf
   * @Description: 文件变更列表
   * @param @param dsId
   * @param @param version
   * @param @param path
   * @param @param name
   * @param @return
   * @param @throws InterruptedException
   * @return String
   * @throws UnsupportedEncodingException
   * @date 2015年10月29日上午8:44:50
   *
   * @修改人 zhangjf
   * @修改日期
   */
  @RequestMapping(value = "/filechanglog/list", method = RequestMethod.GET)
  public String getFileChangeLog(String dsId, String version, String path, String name)
      throws InterruptedException, UnsupportedEncodingException {
    LOG.info("/filechanglog/list ago dsId:" + dsId + "version:" + version + "path:" + path + "name:"
        + name);
    path = java.net.URLDecoder.decode(path, "utf-8");
    name = java.net.URLDecoder.decode(name, "utf-8");
    LOG.info("/filechanglog/list after dsId:" + dsId + "version:" + version + "path:" + path
        + "name:" + name);
    User user = (User) session.getAttribute("user");
    ClientConfiguration.setCustomerId(user.getCustomerId());

    VersionTree tree = VersionTreeUtil.loadVersionTree(dsId, version);

    if (tree == null) {
      List<StorageServer> servers = CommonMethodsUtils.getServer();
      ClientConfiguration.setStorageServers(servers);
      tree = VersionTreeUtil.getVersionTree(dsId, version);
      ZookeeperUtil.close();
    }

    LOG.info("tree:" + tree);
    LOG.info("path:" + path);

    path = path + File.separator + name;
    Gson gson = new Gson();
    LOG.info("/filechanglog/list 111111" + path);
    Node node = tree.findNode(path);

    String customerId = user.getCustomerId();
    
    String parentDsid = UIversionTreeUtil.getParentDsId(node.getParent());
    if(parentDsid!=null){
      dsId = parentDsid;
    }

    if (node instanceof FileNode) {
      FileNode fn = (FileNode) node;
      String fname = fn.getInfo().getPath() + File.separator + fn.getInfo().getName();
      String fid = FileUtil.getFileId(customerId, dsId, fname);
      LOG.info("fid:" + fid);
      String list = ApiUtil.getFileChangeLogs(fid);
      String json = gson.toJson(list);
      System.out.println(json);
      return json;
    }
    return "{}";

  }
}
